package com.wisdytech.linkdcs.account.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.constants.CommonConstants;
import com.wisdytech.common.exception.ServiceException;
import com.wisdytech.common.handler.BaseService;
import com.wisdytech.common.utils.BaseUtil;
import com.wisdytech.linkdcs.account.dao.TmDocumentDao;
import com.wisdytech.linkdcs.account.model.TmDocument;
import com.wisdytech.linkdcs.account.service.ITmDocumentService;
import com.wisdytech.linkdcs.system.model.SysUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

@Service("tmDocumentService")
@Transactional
public class TmDocumentServiceImpl extends BaseService<TmDocumentDao,TmDocument> implements ITmDocumentService {

    @Resource
    TmDocumentDao tmDocumentDao;

    @Override
    public PageInfo<Map<String, Object>> listDocumentByFilter(String equipCode, String equipName, String documentName, Filter filter) {
        Map<String, Object> params = new HashMap<>();
        if(StringUtils.isNotBlank(equipCode)) {
            params.put("equipCode",equipCode);
        }
        if(StringUtils.isNotBlank(equipName)) {
            params.put("equipName",equipName);
        }
        if(StringUtils.isNotBlank(documentName)) {
            params.put("documentName",documentName);
        }
        List<Map<String,Object>> list = this.beforeList(TmDocumentDao.class,filter).listDocumentByFilter(params);
        PageInfo<Map<String, Object>> page = new PageInfo<>(list);
        return page;
    }

    @Override
    public Map<String, Object> getDocumentInfoById(String pkId) {
        Map<String, Object> documentInfo;
        if(StringUtils.isNotBlank(pkId)) {
            documentInfo = tmDocumentDao.getDocumentInfoById(pkId);
            return documentInfo;
        }
        return null;
    }

    @Override
    public void saveOrUpdateDocument(TmDocument tmDocument, SysUser user) {
        //校验操作人
        if(user == null) {
            throw new ServiceException("操作人不能为空");
        }
        if(tmDocument!=null) {
            //检验文档编号
//            if(!StringUtils.isNotBlank(tmDocument.getDocumentCode())) {
//                throw new ServiceException("文档编号不能为空");
//            }
            //修改
            if(StringUtils.isNotBlank(tmDocument.getPkId())) {
                //校验文档编号是否重复
                EntityWrapper<TmDocument> ew =new EntityWrapper<>();
                ew.ne("PK_ID",tmDocument.getPkId());
                ew.eq("EQUIP_CODE",tmDocument.getEquipCode());
                ew.eq("DOCUMENT_NAME",tmDocument.getDocumentName());
                Integer count = tmDocumentDao.selectCount(ew);
                if(count>0) {
                    throw new ServiceException("文档名称重复");
                }
                TmDocument t = tmDocumentDao.selectById(tmDocument.getPkId());
                //文件不同,更新上传日期
                if(t!=null&&(!tmDocument.getFileName().equals(t.getFileName()))) {
                    tmDocument.setUploadDate(new Date());
                }
                tmDocument.setUpdateDate(new Date());
                tmDocument.setUpdateUser(user.getUsername());
                tmDocumentDao.updateDocumentInfo(tmDocument);
            } else {
                //新增
                //校验文档编号是否重复
                EntityWrapper<TmDocument> ew =new EntityWrapper<>();
                ew.eq("EQUIP_CODE",tmDocument.getEquipCode());
                ew.eq("DOCUMENT_NAME",tmDocument.getDocumentName());
                Integer count = tmDocumentDao.selectCount(ew);
                if(count>0) {
                    throw new ServiceException("文档名称重复");
                }
                tmDocument.setDelFlag(CommonConstants.DEL_FLAG);
                tmDocument.setPkId(BaseUtil.getUUID());
                tmDocument.setUploadDate(new Date());
                tmDocument.setCreateDate(new Date());
                tmDocument.setCreateUser(user.getUsername());
                tmDocumentDao.insert(tmDocument);
            }
        }
    }

    @Override
    public void deleteDocument(String pkIds) {
        String[] deletePkIds = pkIds.split(",");
        if(deletePkIds.length == 0) return;
        List<String> pkIdCollection = Arrays.asList(deletePkIds);
        tmDocumentDao.deleteBatchIds(pkIdCollection);
    }
}
